﻿/** 
* 获取本周、本季度、本月、上月的开端日期、停止日期 (月份是当前月份减掉1)
*/
var now = new Date(); //当前日期 
var nowDayOfWeek = now.getDay(); //今天本周的第几天 
var nowDay = now.getDate(); //当前日 
var nowMonth = now.getMonth(); //当前月 
var nowYear = now.getYear(); //当前年 
nowYear += (nowYear < 2000) ? 1900 : 0; // 

var lastMonthDate = new Date(); //上月日期 
lastMonthDate.setDate(1);
lastMonthDate.setMonth(lastMonthDate.getMonth() - 1);
var lastYear = lastMonthDate.getYear();
var lastMonth = lastMonthDate.getMonth();

//格局化日期：yyyy-MM-dd 
function formatDate(date) {
    var myyear = date.getFullYear();
    var mymonth = date.getMonth() + 1;
    var myweekday = date.getDate();

    if (mymonth < 10) {
        mymonth = "0" + mymonth;
    }
    if (myweekday < 10) {
        myweekday = "0" + myweekday;
    }
    return (myyear + "-" + mymonth + "-" + myweekday);
}
function getNowDate() {
    var myDate = new Date();
    var year = myDate.getFullYear();
    var month = parseInt(myDate.getMonth()) + 1;
    var day = parseInt(myDate.getDate());
    if (month < 10) month = "0" + month.toString();
    if (day < 10) day = "0" + day.toString();
    //var date=mydate.toLocaleDateString().split('/').join('-');
    var date = year.toString() + "-" + month + "-" + day;
    return date;
}
//获取前几天的日期 如昨天 day=1
function getPastTime(days){
     var now = new Date();
     var newdate =now.setDate(now.getDate() - 1);
     return newdate;
}
//获得某月的天数 
function getMonthDays(myMonth) {
    var monthStartDate = new Date(nowYear, myMonth, 1);
    var monthEndDate = new Date(nowYear, myMonth + 1, 1);
    var days = (monthEndDate - monthStartDate) / (1000 * 60 * 60 * 24);
    return days;
}

//获得本季度的开端月份 
function getQuarterStartMonth(nowMonth) {
    var quarterStartMonth = 0;
    if (nowMonth < 3) {
        quarterStartMonth = 0;
    }
    if (2 < nowMonth && nowMonth < 6) {
        quarterStartMonth = 3;
    }
    if (5 < nowMonth && nowMonth < 9) {
        quarterStartMonth = 6;
    }
    if (nowMonth > 8) {
        quarterStartMonth = 9;
    }
    return quarterStartMonth;
}

//获得本周的开端日期 
function getWeekStartDate() {
    var weekStartDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek);
    return formatDate(weekStartDate);
}

//获得本周的停止日期 
function getWeekEndDate() {
    var weekEndDate = new Date(nowYear, nowMonth, nowDay + (6 - nowDayOfWeek));
    return formatDate(weekEndDate);
}

//获得本月的开端日期 
function getMonthStartDate() {
    var monthStartDate = new Date(nowYear, nowMonth, 1);
    return formatDate(monthStartDate);
}
//获得某天的开端日期
function getDateMonthStartDate(date) {
    var monthStartDate = new Date(date.getFullYear(), date.getMonth(), 1);
    return formatDate(monthStartDate);
}
//获得本月的停止日期 
function getMonthEndDate() {
    var monthEndDate = new Date(nowYear, nowMonth, getMonthDays(nowMonth));
    return formatDate(monthEndDate);
}

//获得上月开端时候 
function getLastMonthStartDate() {
    var lastMonthStartDate = new Date(nowYear, lastMonth, 1);
    return formatDate(lastMonthStartDate);
}

//获得上月停止时候 
function getLastMonthEndDate() {
    var lastMonthEndDate = new Date(nowYear, lastMonth, getMonthDays(lastMonth));
    return formatDate(lastMonthEndDate);
}

//获得本季度的开端日期 
function getQuarterStartDate(nowYear, nowMonth) {

    var quarterStartDate = new Date(nowYear, getQuarterStartMonth(nowMonth), 1);
    return formatDate(quarterStartDate);
}

//或的本季度的停止日期 
function getQuarterEndDate(nowYear, nowMonth) {
    var quarterEndMonth = getQuarterStartMonth(nowMonth) + 2;
    var quarterStartDate = new Date(nowYear, quarterEndMonth, getMonthDays(quarterEndMonth));
    return formatDate(quarterStartDate);
}
//本季度的天数 
function getQuarterDays(nowYear, nowMonth) {
    var quarterStartMonth = getQuarterStartDate(nowYear, nowMonth);
    var daycount1 = getMonthDays(parseInt(quarterStartMonth));
    var daycount2 = getMonthDays(parseInt(quarterStartMonth) + 1);
    var daycount3 = getMonthDays(parseInt(quarterStartMonth) + 2);
    return daycount1 + daycount2 + daycount3;
}
//本季度已经过去的总天数
function getNowQuarterDays(nowYear, nowMonth, nowDay) {
    var startMonth = getQuarterStartMonth(nowMonth);
    var nowQuarterDays = nowDay;//本月已过的天数
    for (var i = startMonth; i < nowMonth; i++) {
        nowQuarterDays += parseInt(getMonthDays(i));
    }
    return nowQuarterDays;
}
//本年的总天数
function getYearDays(nowYear) {
    nowYearDays = 365;
    if (nowYear % 4 == 0) {
        nowYearDays = 366;
    }
    return nowYearDays;
}
//本年已经过去的总天数
function getNowYearDays(nowYear, nowMonth, nowDay) {
    var nowYearDays = nowDay;
    for (var i = 0; i < nowMonth; i++) {
        nowYearDays += parseInt(getMonthDays(i));
    }
    return nowYearDays;
}


//计算日期差n天
function  DateDiff(sDate1,  sDate2){    //sDate1和sDate2是2002-12-18格式
    var  aDate,  oDate1,  oDate2,  iDays
    aDate  =  sDate1.split("-")
    oDate1  =  new  Date(aDate[1]  +  '-'  +  aDate[2]  +  '-'  +  aDate[0])    //转换为12-18-2002格式
    aDate  =  sDate2.split("-")
    oDate2  =  new  Date(aDate[1]  +  '-'  +  aDate[2]  +  '-'  +  aDate[0])
    iDays  =  parseInt(Math.abs(oDate1  -  oDate2)  /  1000  /  60  /  60  /24)    //把相差的毫秒数转换为天数
    console.log(iDays)
    return  iDays
}
//时间格式化 yyyy-MM-dd HH:mm:ss
function formatTime(date,fmt) {
    var o = {
        "M+" : date.getMonth()+1, //月份
        "d+" : date.getDate(), //日
        "h+" : date.getHours()%12 == 0 ? 12 : date.getHours()%12, //小时
        "H+" : date.getHours(), //小时
        "m+" : date.getMinutes(), //分
        "s+" : date.getSeconds(), //秒
        "q+" : Math.floor((date.getMonth()+3)/3), //季度
        "S" : date.getMilliseconds() //毫秒
    };
    var week = {
        "0" : "/u65e5",
        "1" : "/u4e00",
        "2" : "/u4e8c",
        "3" : "/u4e09",
        "4" : "/u56db",
        "5" : "/u4e94",
        "6" : "/u516d"
    };
    if(/(y+)/.test(fmt)){
        fmt=fmt.replace(RegExp.$1, (date.getFullYear()+"").substr(4 - RegExp.$1.length));
    }
    if(/(E+)/.test(fmt)){
        fmt=fmt.replace(RegExp.$1, ((RegExp.$1.length>1) ? (RegExp.$1.length>2 ? "/u661f/u671f" : "/u5468") : "")+week[date.getDay()+""]);
    }
    for(var k in o){
        if(new RegExp("("+ k +")").test(fmt)){
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
        }
    }
    return fmt;
}
//获取时间转成n时n分n秒
function timeDiff(startTime,endTime){
    var second=parseInt((new Date(endTime)-new Date(startTime))/1000);
    secondFomatTime(second);
}
//n秒转n时n分n秒
function secondFomatTime(second){
    var h=0;
    var m=0;
    var s=0;
    if(second >=3600){
        h=Math.floor(second/3600);
    }
    if(second%3600>=60){
        m=Math.floor((second%3600)/60);
    }
    var s =(second%3600)%60;
    var time="";
    if(h==0 && m==0 && s==0){
        time="0秒"
    }
    else {
        if (h != 0) {
            time += h + "时";
        }
        if (m != 0) {
            time += m + "分";
        }
        if (s != 0) {
            time += s + "秒";
        }
    }
    return time;
}
//根据时间差得到未来时间
function DateAdd(strInterval, Number,nowDate){
    var dtTmp;
    if(nowDate ==undefined || nowDate==""){
        dtTmp= new Date();
    }
    else {
        dtTmp = nowDate;
    }
    switch (strInterval) {
        case 's' :return new Date(Date.parse(dtTmp) + (1000 * Number));
        case 'n' :return new Date(Date.parse(dtTmp) + (60000 * Number));
        case 'h' :return new Date(Date.parse(dtTmp) + (3600000 * Number));
        case 'd' :return new Date(Date.parse(dtTmp) + (86400000 * Number));
        case 'w' :return new Date(Date.parse(dtTmp) + ((86400000 * 7) * Number));
        case 'q' :return new Date(dtTmp.getFullYear(), (dtTmp.getMonth()) + Number*3, dtTmp.getDate(), dtTmp.getHours(), dtTmp.getMinutes(), dtTmp.getSeconds());
        case 'm' :return new Date(dtTmp.getFullYear(), (dtTmp.getMonth()) + Number, dtTmp.getDate(), dtTmp.getHours(), dtTmp.getMinutes(), dtTmp.getSeconds());
        case 'y' :return new Date((dtTmp.getFullYear() + Number), dtTmp.getMonth(), dtTmp.getDate(), dtTmp.getHours(), dtTmp.getMinutes(), dtTmp.getSeconds());
    }
}

//获取当前星期几
function getWeek(){
    var a = new Array("日", "一", "二", "三", "四", "五", "六");  
    return "星期"+a[nowDayOfWeek];
}
